<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>后台商品管理</title>
	<link rel="stylesheet" href="css/reset.css" />
	<link rel="stylesheet" href="css/content.css" />
	<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
	<style>
		.label_style{
			color:#333;
		}
	</style>
	
	<script type="text/javascript">
	//创建并返回异步请求对象
    	
			var currpage = '${sessionScope.goodspage.currentpage}';
			var total = '${sessionScope.goodspage.totalpage}';
	
    $(function(){
    //alert(1);
		$("#nextpage").click(function(){
			//alert(currpage);
			//alert(total);
					if(currpage==total){
						return;
					}else{
					//alert(parseInt(currpage));
						currpage=parseInt(currpage)+1;
					}
			//alert(currpage);
			page(currpage);
			$("#nowpage").text(currpage);
		});	
		
		$("#prepage").click(function(){
			//alert(currpage);
			//alert(total);
					if(currpage==1){
					//alert(parseInt(currpage));
						return;
					}else{
						currpage=parseInt(currpage)-1;
					
					}
			//alert(parseInt(currpage));
			page(currpage);
			$("#nowpage").text(currpage);
		});	
		
			
	function page(pg){
		//alert(pg);
			$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath }/ajax/pagequerygoodsajax',
				data:{
					curr:pg,
					name:$("#searchadminname").val(),
					type:$("#select_class").val()
				},
				success:function(response,status,xhr){
					var jsonobj = eval('('+response+')');
					var lengthlist = jsonobj.goodslist.length;
		    		//alert(lengthlist);
		    		
		    		$("#tb").empty();
					var tbobj = document.getElementById("tb");
						
						for(var i=0;i<lengthlist;i++){
						//alert(jsonobj.goodslist[i].goodsimg);
							//alert(jsonobj.goodslist[i].goodsname);
								var optTr = document.createElement("tr");
				    		    //var imgobj = document.createElement("img");
				    		    var optObj1 = document.createElement("td");
				    		    var optObj2 = document.createElement("td");
				    		    var optObj3 = document.createElement("td");
				    		    var optObj4 = document.createElement("td");
				    		    var optObj5 = document.createElement("td");
				    		    var optObj6 = document.createElement("td");
				    		    var optObj7 = document.createElement("td");
				    		    var optObj8 = document.createElement("td");
				    		    var optObj9 = document.createElement("td");
				    		    var optObj10 = document.createElement("td");
								optObj1.innerHTML=jsonobj.goodslist[i].goodsid;
				    		    optObj2.innerHTML=jsonobj.goodslist[i].goodsname;
				    		     optObj3.innerHTML="<img src='${pageContext.request.contextPath }/"+jsonobj.goodslist[i].goodsimg+"' width='200px' height='200px'/>";
				    		    optObj4.innerHTML=jsonobj.goodslist[i].issuetime;
				    		    optObj5.innerHTML=jsonobj.goodslist[i].username;
				    		    optObj6.innerHTML=jsonobj.goodslist[i].goodsprice;
				    		    optObj7.innerHTML=jsonobj.goodslist[i].goodsnumber;
				    		    optObj8.innerHTML=jsonobj.goodslist[i].goodsPageview;
				    		    optObj9.innerHTML=jsonobj.goodslist[i].typename;
				    		    optObj10.innerHTML="<div class='table-fun'>"+
												"<a onclick='goodsinfo("+jsonobj.goodslist[i].goodsid+");' style='cursor:pointer'>详情</a>"+
												"<a onclick='remove("+jsonobj.goodslist[i].goodsid+");' style='cursor:pointer'>删除</a></div>";
				    		    //optobj3.appendChild(imgobj);
				    		    optTr.appendChild(optObj1);
				    		    optTr.appendChild(optObj2);
    		    				optTr.appendChild(optObj3);
				    		    optTr.appendChild(optObj4);
				    		    optTr.appendChild(optObj5);
				    		    optTr.appendChild(optObj6);
				    		    optTr.appendChild(optObj7);
				    		    optTr.appendChild(optObj8);
				    		    optTr.appendChild(optObj9);
				    		    optTr.appendChild(optObj10);
				    		    tbobj.appendChild(optTr);
						}	
			
				}
			});		
	}
	
	
		$("#select_class").change(function(){
				$.ajax({
					type:'post',
					url:'${pageContext.request.contextPath}/ajax/typenamequerygoodsajax',
					data:{
						curr:1,
						type:$("#select_class").val(),
						name:$("#searchname").val()
					},
					
				    success:function(response,status,xhr){
						var jsonobj = eval('('+response+')');
						var lengthlist = jsonobj.goodslist.length;
						//alert(lengthlist);
						
							$("#tb").empty();
						var tbobj = document.getElementById("tb");
						
						for(var i=0;i<lengthlist;i++){
						//alert(jsonobj.goodslist[i].goodsimg);
							//alert(jsonobj.goodslist[i].goodsname);
								var optTr = document.createElement("tr");
				    		    //var imgobj = document.createElement("img");
				    		    var optObj1 = document.createElement("td");
				    		    var optObj2 = document.createElement("td");
				    		    var optObj3 = document.createElement("td");
				    		    var optObj4 = document.createElement("td");
				    		    var optObj5 = document.createElement("td");
				    		    var optObj6 = document.createElement("td");
				    		    var optObj7 = document.createElement("td");
				    		    var optObj8 = document.createElement("td");
				    		    var optObj9 = document.createElement("td");
				    		    var optObj10 = document.createElement("td");
								optObj1.innerHTML=jsonobj.goodslist[i].goodsid;
				    		    optObj2.innerHTML=jsonobj.goodslist[i].goodsname;
				    		     optObj3.innerHTML="<img src='${pageContext.request.contextPath }/"+jsonobj.goodslist[i].goodsimg+"' width='200px' height='200px'/>";
				    		    optObj4.innerHTML=jsonobj.goodslist[i].issuetime;
				    		    optObj5.innerHTML=jsonobj.goodslist[i].username;
				    		    optObj6.innerHTML=jsonobj.goodslist[i].goodsprice;
				    		    optObj7.innerHTML=jsonobj.goodslist[i].goodsnumber;
				    		    optObj8.innerHTML=jsonobj.goodslist[i].goodsPageview;
				    		    optObj9.innerHTML=jsonobj.goodslist[i].typename;
				    		    optObj10.innerHTML="<div class='table-fun'>"+
												"<a onclick='goodsinfo("+jsonobj.goodslist[i].goodsid+");' style='cursor:pointer'>详情</a>"+
												"<a onclick='remove("+jsonobj.goodslist[i].goodsid+");' style='cursor:pointer'>删除</a></div>";
				    		    //optobj3.appendChild(imgobj);
				    		    optTr.appendChild(optObj1);
				    		    optTr.appendChild(optObj2);
    		    				optTr.appendChild(optObj3);
				    		    optTr.appendChild(optObj4);
				    		    optTr.appendChild(optObj5);
				    		    optTr.appendChild(optObj6);
				    		    optTr.appendChild(optObj7);
				    		    optTr.appendChild(optObj8);
				    		    optTr.appendChild(optObj9);
				    		    optTr.appendChild(optObj10);
				    		    tbobj.appendChild(optTr);
						}	
						
					}	
			});
			});
			
			$("#searchname").keyup(function(){
				$.ajax({
					type:'post',
					url:'${pageContext.request.contextPath}/ajax/typenamequerygoodsajax',
					data:{
						curr:1,
						type:$("#select_class").val(),
						name:$("#searchname").val()
					},
					
				    success:function(response,status,xhr){
						var jsonobj = eval('('+response+')');
						
						var lengthlist = jsonobj.goodslist.length;
						//alert(lengthlist);
						if(lengthlist==0){
							alert("您查询的商品不存在！！！");
						}else{
							$("#tb").empty();
							var tbobj = document.getElementById("tb");
							
							for(var i=0;i<lengthlist;i++){
								//alert(jsonobj.goodslist[i].goodsname);
									var optTr = document.createElement("tr");
					    		    var imgobj = document.createElement("img");
					    		    var optObj1 = document.createElement("td");
					    		    var optObj2 = document.createElement("td");
					    		    var optObj3 = document.createElement("td");
					    		    var optObj4 = document.createElement("td");
					    		    var optObj5 = document.createElement("td");
					    		    var optObj6 = document.createElement("td");
					    		    var optObj7 = document.createElement("td");
					    		    var optObj8 = document.createElement("td");
					    		    var optObj9 = document.createElement("td");
					    		    var optObj10 = document.createElement("td");
									optObj1.innerHTML=jsonobj.goodslist[i].goodsid;
					    		    optObj2.innerHTML=jsonobj.goodslist[i].goodsname;
					    		     optObj3.innerHTML="<img src='${pageContext.request.contextPath }/"+jsonobj.goodslist[i].goodsimg+"' width='200px' height='200px'/>";
					    		    optObj4.innerHTML=jsonobj.goodslist[i].issuetime;
					    		    optObj5.innerHTML=jsonobj.goodslist[i].username;
					    		    optObj6.innerHTML=jsonobj.goodslist[i].goodsprice;
					    		    optObj7.innerHTML=jsonobj.goodslist[i].goodsnumber;
					    		    optObj8.innerHTML=jsonobj.goodslist[i].goodsPageview;
					    		    optObj9.innerHTML=jsonobj.goodslist[i].typename;
					    		    optObj10.innerHTML="<div class='table-fun'>"+
													"<a onclick='goodsinfo("+jsonobj.goodslist[i].goodsid+");' style='cursor:pointer'>详情</a>"+
													"<a onclick='remove("+jsonobj.goodslist[i].goodsid+");' style='cursor:pointer'>删除</a></div>";
					    		    optTr.appendChild(optObj1);
					    		    optTr.appendChild(optObj2);
	    		    				optTr.appendChild(optObj3);
					    		    optTr.appendChild(optObj4);
					    		    optTr.appendChild(optObj5);
					    		    optTr.appendChild(optObj6);
					    		    optTr.appendChild(optObj7);
					    		    optTr.appendChild(optObj8);
					    		    optTr.appendChild(optObj9);
					    		    optTr.appendChild(optObj10);
					    		    tbobj.appendChild(optTr);
							}	
						}
						
					}	
			});
			
			});

			
		});
		
		
		function remove(id){
    		if(confirm("您确定要删除编号为  ["+id+"]  的商品???")){
    			$.ajax({
    				type:'post',
    				url:'${pageContext.request.contextPath}/ajax/removegoodsajax',
    				data:{
    					goodsid:id
    				},
    				success:function(response,status,xhr){
   						alert("删除成功");
   						page(1);
    				}
    			});
    		}
    	}
    	
    	function goodsinfo(id){
    	var url = "${pageContext.request.contextPath}/servlet/selectgoodsservlet?goodsid="+id;
    		window.location.href=url;
    	}
	</script>
</head>
<body marginwidth="0" marginheight="0">
	<div class="container">
		<div class="public-nav">您当前的位置：<a href="">管理首页</a>><a href="">商品管理</a></div>
		<div class="public-content">
			<div class="public-content-header">
				<h3>商品列表
				<span style="float:right;margin-right:15px;">
					<label class="label_style">分类：</label>
					<select name="select" id="select_class" class="form-select">
						<option value="">选择分类</option>
						<option value="家用">家用</option>
						<option value="电子产品">电子产品</option>
						<option value="书籍">书籍</option>
						<option value="生活用品">生活用品</option>
					</select>
					<label class="label_style">搜索：</label>
					<input type="text" placeholder="搜索用户名称" id="searchname" style="height:20px"/>
				</span>
				</h3>
			</div>
			<div class="public-content-cont">
				<table class="public-cont-table">
					<tr>
						<th style="width:9%">商品编号</th>
						<th style="width:9%">商品名称</th>
						<th style="width:18%">商品图片</th>
						<th style="width:5%">发布时间</th>
						<th style="width:10%">用户名称</th>
						<th style="width:7%">商品单价</th>
						<th style="width:10%">库存数量</th>
						<th style="width:5%">点击量</th>
						<th style="width:9%">所属分类</th>
						<th style="width:10%">操作</th>
					</tr>
					<tbody id="tb">
					<c:forEach items="${sessionScope.goodspage.goodslist}" var="goods" varStatus="a">
					<tr>
						<td>${goods.goodsid}</td>
						<td>${goods.goodsname}</td>
						<td id="tdimg"><img width="200px" height="200px" src="${pageContext.request.contextPath }/${goods.goodsimg}"/></td>
						<td>${goods.issuetime}</td>
						<td>${goods.username}</td>
						<td>${goods.goodsprice}</td>
						<td>${goods.goodsnumber}</td>
						<td>${goods.goodsPageview}</td>
						<td>${goods.typename}</td>
						<td>
							<div class="table-fun">
								<a onclick="goodsinfo('${goods.goodsid}');" style="cursor:pointer">详情</a>
								<a onclick="remove('${goods.goodsid}');" style="cursor:pointer">删除</a>
							</div>
						</td>
					</tr>
					</c:forEach>
					</tbody>
				</table>
				<div class="page">
						<a id="firstpage" style="cursor:pointer;">首页</a>
						<a id="prepage" style="cursor:pointer;">上一页</a>
						<a id="nextpage" style="cursor:pointer;">下一页</a>
						第<span style="color:red;font-weight:600" id="nowpage">1</span>页
						共<span style="color:red;font-weight:600" id="ttpage">${sessionScope.goodspage.totalpage}</span>页
						<input id="pagecurr" type="text" class="page-input">
						<input type="submit" class="page-btn" value="跳转" onclick="skip();">
				</div>
			</div>
		</div>
	</div>
</body>
</html>